package services

import (
	"errors"
	"lingc/models"
	"lingc/utils"
	"log"
	"strconv"

	"gorm.io/gorm"
)

// SysLangItemService 提供账号登录校验的服务
type SysLangItemService struct {
	DB *gorm.DB
}

// NewSysLangItemService 创建一个新的 SysLangItemService 实例
func NewSysLangItemService(db *gorm.DB) *SysLangItemService {
	return &SysLangItemService{DB: db}
}

//表单业务

/**
 * @Description: 分页获取列表
 * @param where
 * @param page
 * @param pageSize
 * @return []*models.SysLangItem
 * @return int
 * @return int64
 * @return error
 */
func (s *SysLangItemService) PageSysLangItemList(where string, page, pageSize int) ([]map[string]interface{}, int, int64, error) {
	var groups []*models.SysLangItem
	var total int64

	// 计算偏移量
	offset := (page - 1) * pageSize

	// 获取总记录数
	if err := s.DB.Model(&models.SysLangItem{}).Where(where).Count(&total).Error; err != nil {
		return nil, 1, 0, err
	}

	// 获取分页数据
	if err := s.DB.Where(where).Offset(offset).Limit(pageSize).Order("id DESC").Find(&groups).Error; err != nil {
		return nil, 1, 0, err
	}
	list := make([]map[string]interface{}, 0)
	for _, data := range groups {
		detail_list, _ := NewSysLangDetailService(utils.MysqlClient).SysLangDetailList(data.ID)
		item_data := map[string]interface{}{
			"id":    data.ID,
			"title": data.Title,
		}
		for _, item := range detail_list {
			key := "lang_" + strconv.Itoa(int(item.LangID))
			item_data[key] = item.Text
		}
		list = append(list, item_data)
	}

	return list, page, total, nil
}

// GetLangItemList 获取条目列表
func (s *SysLangItemService) GetLangItemList() (map[uint]string, error) {
	var list_data []*models.SysLangItem
	list := make(map[uint]string)
	if err := s.DB.Find(&list_data).Error; err != nil {
		return list, err
	}
	for _, data := range list_data {
		list[data.ID] = data.Title
	}
	return list, nil

}

/**
 * @Description: 创建新
 * @param username
 * @param password
 * @param realname
 * @param avatar
 * @param status
 * @return *models.SysLangItemCreate
 * @return error
 */
func (s *SysLangItemService) CreateSysLangItem(data *models.SysLangItemCreate, detail string) ([]uint, error) {

	/*
		Title     string `json:"title"`
	*/
	addData := &models.SysLangItem{
		Title: data.Title,
	}
	// 保存到数据库
	if err := s.DB.Create(addData).Error; err != nil {
		return nil, err
	}

	ids, err := NewSysLangDetailService(utils.MysqlClient).CreateSysLangDetails(addData, detail)
	if err != nil {
		return nil, err
	}
	return ids, nil
}

// GetSysLangItem 获取信息
func (s *SysLangItemService) GetSysLangItem(where string) (*models.SysLangItem, error) {
	var data models.SysLangItem
	if err := s.DB.Where(where).First(&data).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("数据不存在")
		}
		return nil, err
	}
	// log.Printf("data_info:%v", data)
	return &data, nil
}

/**
 * @Description: 更新信息
 * @param data
 * @return error
 */
func (s *SysLangItemService) UpdateSysLangItem(data *models.SysLangItemUpdate, detail string) ([]uint, error) {
	// 检查名是否已存在
	var existingData models.SysLangItem
	if err := s.DB.Where("id = ?", data.ID).First(&existingData).Error; err != nil {
		return nil, errors.New("数据不存在")
	}
	/*
		Title     string `json:"title"`
		AppID     int    `json:"app_id"`
	*/
	if existingData.Title != data.Title {
		// 检查名是否已存在
		existingData.Title = data.Title
		err := s.DB.Save(existingData).Error
		if err != nil {
			return nil, err
		}
	}
	log.Printf("data_info:%v", detail)
	ids, err := NewSysLangDetailService(utils.MysqlClient).UpdateSysLangDetails(detail)
	if err != nil {
		return nil, err
	}
	return ids, nil

}

/**
 * @Description: 删除
 * @param id
 * @return error
 */
func (s *SysLangItemService) DeleteSysLangItem(id uint) error {
	var data models.SysLangItem
	if err := s.DB.Where("id = ?", id).First(&data).Error; err != nil {
		return errors.New("数据不存在")
	}
	NewSysLangDetailService(utils.MysqlClient).DeleteSysLangDetail(id)
	return s.DB.Delete(&data).Error
}

/**
 * @Description: 批量删除
 * @param id
 * @return error
 */
func (s *SysLangItemService) DeleteSysLangItems(ids []string) error {

	for _, id := range ids {
		var data models.SysLangItem
		if err := s.DB.Where("id = ?", id).First(&data).Error; err != nil {
			return errors.New("数据不存在")
		}
		err := s.DB.Delete(&data).Error
		if err != nil {
			return errors.New("删除失败")
		}
	}
	NewSysLangDetailService(utils.MysqlClient).DeleteSysLangDetails(ids)
	return nil
}
